package com.poet.quick.ssh.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.poet.quick.ssh.entity.Grade;
import com.poet.quick.ssh.entity.User;
import com.poet.quick.ssh.service.GradeService;
import com.poet.quick.ssh.service.UserService;

@Controller
@RequestMapping("/")
public class DemoController {

	@Resource
	private UserService userService;
	@Resource
	private GradeService gradeService;
	
	@RequestMapping("/demo")
	@ResponseBody
	public String demo(){
		return "abc";
	}
	
	@RequestMapping("/encoding")
	@ResponseBody
	public String encoding(@RequestParam(required=true) String str){
		return str;
	}
	
	@RequestMapping(value="/allUsers",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String allUser(){
		List<User> users = userService.findAll();
		String json = JSON.toJSONString(users);
		return json;
	}
	
	@RequestMapping(value="/allGrades",produces="application/json;charset=UTF-8")
	@ResponseBody
	public String allGrades(){
		List<Grade> grades = gradeService.findAll();
		String json = JSON.toJSONString(grades);
		return json;
	}
	
	@RequestMapping(value="/saveGrade")
	@ResponseBody
	public String saveGrade(){
		Grade grade = new Grade();
		grade.setGradeName("初二");
		grade.setGraderLeader("黎明");
		gradeService.save(grade);
		return grade.getId().toString();
	}
}
